package net.dopan.pigframe.config.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @author shiyz
 * @creation date 2012-10-25 上午13:01:21
 */
public  class  Sort {
    public static final String REGEX_CHINESE = "^[\\u4e00-\\u9fa5]*$";
    public static final String REGEX_ENGLISH= "[a-zA-Z]+";
    public static void main(String[] args) {
        Sort obj1 = new Sort();
        System.out.println("======================");
        List<Map<String,String>> lists= new ArrayList<>();
        Map<String,String> map = new HashMap<>();
        map.put("concern_username","大家好");
        map.put("kkk","kakak");
        Map<String,String> map1 = new HashMap<>();
        map1.put("concern_username","家好啊");
        map.put("kkk","kakcds");
        lists.add(map);
        lists.add(map1);
        Map maps = Sort.sort(lists);
        System.err.println(maps);
        Sort sort = new Sort();
        String str = "你好啊";
        System.err.println(str.substring(0,1));
        String string = "a";
        System.err.println(string.substring(0,1));
    }

    public Sort() {

    }

    //字母Z使用了两个标签，这里有２７个值
    //i, u, v都不做声母, 跟随前面的字母
    private static char[] chartable =
            {
                    '啊', '芭', '擦', '搭', '蛾', '发', '噶', '哈', '哈',
                    '击', '喀', '垃', '妈', '拿', '哦', '啪', '期', '然',
                    '撒', '塌', '塌', '塌', '挖', '昔', '压', '匝', '座'
            };
    private static char[] alphatableb =
            {
                    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
                    'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
            };
    private static char[] alphatables =
            {
                    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                    'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
            };
    private static int[] table = new int[27];  //初始化

    {
        for (int i = 0; i < 27; ++i) {
            table[i] = gbValue(chartable[i]);
        }
    }

    //主函数,输入字符,得到他的声母,
    //英文字母返回对应的大小写字母
    //其他非简体汉字返回 '0'  按参数
    public static char Char2Alpha(char ch, String type) {
        if (ch >= 'a' && ch <= 'z')
            return (char) (ch - 'a' + 'A');//为了按字母排序先返回大写字母
        // return ch;
        if (ch >= 'A' && ch <= 'Z')
            return ch;

        int gb = gbValue(ch);
        if (gb < table[0])
            return '#';

        int i;
        for (i = 0; i < 26; ++i) {
            if (match(i, gb))
                break;
        }

        if (i >= 26) {
            return '#';
        } else {
            if ("b".equals(type)) {//大写
                return alphatableb[i];
            } else {//小写
                return alphatables[i];
            }
        }
    }

    //根据一个包含汉字的字符串返回第一个汉字拼音首字母的字符串
    public static String String2AlphaFirst(String SourceStr, String type) {
        if(!Pattern.matches(REGEX_ENGLISH,SourceStr)&!Pattern.matches(REGEX_CHINESE,SourceStr))
            return "#";
            String Result = "";

            try {
                Result += Char2Alpha(SourceStr.charAt(0), type);
            } catch (Exception e) {
                Result = "";
            }
            return Result;
        }


    private static boolean match(int i, int gb) {
        if (gb < table[i])
            return false;
        int j = i + 1;

        //字母Z使用了两个标签
        while (j < 26 && (table[j] == table[i]))
            ++j;
        if (j == 26)
            return gb <= table[j];
        else
            return gb < table[j];
    }

    //取出汉字的编码
    private static int gbValue(char ch) {
        String str = new String();
        str += ch;
        try {
            byte[] bytes = str.getBytes("GBK");
            if (bytes.length < 2)
                return 0;
            return (bytes[0] << 8 & 0xff00) + (bytes[1] &
                    0xff);
        } catch (Exception e) {
            return 0;
        }
    }

    public static Map sort(List<Map<String,String>> list) {
        Map map = new HashMap();
        ArrayList<Map<String,String>> arraylist = new ArrayList();
        int j = list.size();
        String[] alphatableb =
                {
                        "A", "B", "C", "D", "E", "F", "G", "H", "I",
                        "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z","#"
                };
        for (String a : alphatableb) {
//            if(j<=0) break;
            for (int i = 0; i < list.size(); i++) {
                if (a.equals(Sort.String2AlphaFirst(list.get(i).get("fans_username").substring(0,1), "b"))) {
                    arraylist.add(list.get(i));
                    j--;
                }
            }
            map.put(a, arraylist);
            arraylist = new ArrayList<Map<String,String>>();
        }
        return map;
    }
    public static Map sortFans(List<Map<String,String>> list) {
        Map map = new HashMap();
        ArrayList<Map<String,String>> arraylist = new ArrayList();
        int j = list.size();
        String[] alphatableb =
                {
                        "A", "B", "C", "D", "E", "F", "G", "H", "I",
                        "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z","#"
                };
        for (String a : alphatableb) {
//            if(j<=0) break;
            for (int i = 0; i < list.size(); i++) {
                if (a.equals(Sort.String2AlphaFirst(list.get(i).get("fans_username").substring(0,1), "b"))) {
                    arraylist.add(list.get(i));
                    j--;
                }
            }
            map.put(a, arraylist);
            arraylist = new ArrayList<Map<String,String>>();
        }
        return map;
    }
}
